﻿/************************ (C) COPYLEFT 2018 Merafour *************************
* File Name          : Ini_data.c
* Author             : Merafour
* Last Modified Date : 04/05/2020
* Description        : 配置文件解析.
********************************************************************************
* https://merafour.blog.163.com
* merafour@163.com
* https://github.com/merafour
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Ini.h"
#include "mIni.h"
//#include <assert.h>
//#include <errno.h>
//#include <ctype.h>
//#include <stdarg.h>
/*   获取一一个整数   */
int ini_get_int(struct ini_Parse* const Ini, const char section[], const char _key[], const int dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    int value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "%8d", dft);
    int ret = ini_get_field(Ini, section, _key, _dft, _value);
    if(0==ret)
    {
        // char to int
        value = atoi(_value);
    }
    return value;
}
int mIni_get_int(const char Ini[], const uint16_t iSize, const char section[], const char _key[], const int dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    int value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "%8d", dft);
    int ret = mIni_get_field(Ini, iSize, section, _key, _dft, _value);
    if(0==ret)
    {
        // char to int
        value = atoi(_value);
    }
    return value;
}
/*   获取一一个double数据   */
double ini_get_double(struct ini_Parse* const Ini, const char section[], const char _key[], const double dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    double value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "%8f", dft);
    int ret = ini_get_field(Ini, section, _key, _dft, _value);
    if(0==ret)
    {
        // char to int
        value = atof(_value);
    }
    return value;
}
double mIni_get_double(const char Ini[], const uint16_t iSize, const char section[], const char _key[], const double dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    double value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "%8f", dft);
    int ret = mIni_get_field(Ini, iSize, section, _key, _dft, _value);
    if(0==ret)
    {
        // char to int
        value = atof(_value);
    }
    return value;
}
//字符串转十六进制
static inline int _hextoi(const char ch)
{
    if((ch>='0') && (ch<='9')) return (ch-'0');
    if((ch>='a') && (ch<='f')) return (ch-'a'+0x0A);
    if((ch>='A') && (ch<='F')) return (ch-'A'+0x0A);
    return 0;
}
static int hextoi(const char str[])
{
    int len = strlen(str);
    int i=0;
    int value=0;
    if(len>10) len=10;
    for(i=0,value=0; i<len; i++)
    {
        value <<= 4;
        value |= (_hextoi(str[i])&0x0F);
    }
    return value;
}
/*   获取一个十六进制数:0x--   */
int ini_get_hex(struct ini_Parse* const Ini, const char section[], const char _key[], const int dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    int value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "0x%8X", dft);
    int ret = ini_get_field(Ini, section, _key, _dft, _value);
    if(0==ret)
    {
        // hex to int
        value = hextoi(_value);
    }
    return value;
}
int mIni_get_hex(const char Ini[], const uint16_t iSize, const char section[], const char _key[], const int dft)
{
    char _value[INI_ITEM_LEN];
    char _dft[32];
    int value=dft;
    memset(_value, 0, sizeof(_value));
    memset(_dft, 0, sizeof(_dft));
    snprintf(_dft, sizeof(_dft)-1, "0x%8X", dft);
    int ret = mIni_get_field(Ini, iSize, section, _key, _dft, _value);
    if(0==ret)
    {
        // hex to int
        value = hextoi(_value);
    }
    return value;
}

